<div class="warpper">
    <div class="title">文章 - 文章分类</div>
    <div class="content">
        <div class="flexilist">
            <div class="common-head">
                <div class="fl">
                    <div class="fbutton" id="url" data-url="${ctx}/admin/user/add.html"><div class="add" title="添加分类"><span><i class="icon icon-plus"></i>添加分类</span></div></div>
                    <div class="fbutton" id="url" data-url="${ctx}/admin/user/add.html"><div class="add" title="一键静态化"><span><i class="icon icon-plus"></i>一键静态化</span></div></div>
                </div>
            </div>
            <div class="common-content">
                <div class="list-div" id="listDiv">
                    <table cellpadding="0" cellspacing="0" border="0" data-cols="13">
                        <thead>
                        <tr>
                            <th width="6%">ID</th>
                            <th width="6%">排序</th>
                            <th width="10%">名称</th>
                            <th width="15%">样式</th>
                            <th width="12%">目录</th>
                            <th width="8%">模板</th>
                            <th width="5%">类型</th>
                            <th width="5%">导航</th>
                            <th width="5%">数据</th>
                            <th width="5%">状态</th>
                            <th width="5%"><div class="handle">操作</div></th>
                        </tr>
                        </thead>
                        <tbody></tbody>
                        <script id="table_template" type="text/x-jquery-tmpl">
                        <tr cid="{{= catid}}" {{if parentid!=0}} pid="{{= parentid}}" {{/if}}>
                            <td>{{= catid}}</td>
                            <td><input type="text" class="text sort" value="{{= sort}}" style="width:30px;"/></td>
                            <td {{if leaf==false || parentid==0}}class="act collapsed"{{/if}}>
                                {{if leaf && parentid!=0}}<i class="kg25"><img src="${IMG_PATH}/file.gif" class="mpp5"/> {{= catname}}</i>
                                {{else child==0 && leaf==false}}
                                    <img src="${IMG_PATH}/folder.gif" class="mpp5"/> {{= catname}}
                                {{else}}
                                    <img src="${IMG_PATH}/folder-open.gif" class="mpp5"/> {{= catname}}
                                {{/if}}
                            </td>
                            <td>{{= style}}</td>
                            <td>{{= catdir}}</td>
                            <td>{{= template}}</td>
                            <td>{{if type}}内置{{else type==0}}<span style="color:red">外链</span>{{else type==2}}<span style="color:blue">单页</span>{{/if}}</td>
                            <td><img src="${IMG_PATH}/{{if nav}}yes.gif{{else}}no.gif{{/if}}" style="vertical-align:middle;"/></td>
                            <td>100</td>
                            <td>
                                <div class="switch {{if status}}active{{/if}}">
                                    <div class="circle"></div>
                                    <input type="hidden" value="{{if status}}1{{else}}0{{/if}}">
                                </div>
                            </td>
                            <td>
                                <div class="handle">
                                    <div class="tDiv a4">
                                        <a href="javascript:void(0);" title="编辑" class="btn_see"><i class="icon icon-plus"></i>添加子类</a>
                                        <a href="javascript:void(0);" title="编辑" class="btn_see"><i class="icon icon-plus"></i>生成静态</a>
                                        <a href="javascript:void(0);" title="编辑" class="btn_edit"><i class="icon icon-edit"></i>编辑</a>
                                        <a href="javascript:del('{{= catid}}');" title="移除" class="btn_trash"><i class="icon icon-trash"></i>移除</a>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        </script>
                        <tfoot>
                        <tr>
                            <td colspan="13">
                                <div class="fl">
                                    <div class="refresh">
                                        <div class="refresh_tit" title="刷新数据"><i class="icon icon-spin icon-refresh"></i></div>
                                        <div class="refresh_span">刷新 - 共 <span class="rows">0</span> 条记录</div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        </tfoot>
                    </table>

                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    function initData(){
        $.get("${ctx}/category/list", function(res){
            if(res.code==0){
                $("div.list-div tbody").empty();
                $("div.refresh_span>.rows").text(res.data.length);
                if(res.data.length == 0) {
                    var cols = $("div.list-div tbody").parent().attr("data-cols");
                    $("div.list-div tbody").html('<tr class="tr_bg_blue"><td class="no-records" colspan="'+cols+'">没有找到任何记录</td></tr>');
                }else
                    $("#table_template").tmpl(res.data).appendTo("div.list-div tbody");
                setTimeout(function () {
                    $("div.refresh_tit>i").removeClass("icon-spin");
                }, 1000);
            }else{
                alert("获取文章分类列表失败!");
            }
        });
    }


    //删除分类
    function del(id){
        if(confirm("您是否确定移除该分类？")){
            $.post("${ctx}/category/delete?id="+id, function(res){
                if(res.code == 0)
                    initData();
                else
                    alert(res.message);
            });
        }
    }

    $(function(){
        initData();
        $("div.list-div").on("click", "td.act", function(){
            var cid = $(this).parent("tr").attr("cid");
            var trs = $("div.list-div").find("tr[pid='"+cid+"']");
            if(trs.length!=0){
                if($(this).hasClass("collapsed")){
                    $(this).find("img").attr("src", "${IMG_PATH}/folder.gif");
                    $(this).removeClass("collapsed");
                }else{
                    $(this).find("img").attr("src", "${IMG_PATH}/folder-open.gif");
                    $(this).addClass("collapsed");
                }

                trs.toggle();
            }
        });

        $("div.list-div").on("blur","input.sort", function(){
            var sort = $(this).val();
            var id = $(this).parents("tr").attr("cid");
            if(/^\d+$/.test(sort)){
                $.post("${ctx}/category/updateSort",{id:id, sort:sort}, function(res){
                    if(res.code == 0) {
                        alert("修改成功!");
                        initData();
                    }else
                        alert(res.message);
                });
            }else
                alert("请输入数字!");
        });
    });
</script>